package com.xoa.controller.workflow;

import com.alibaba.fastjson.JSONObject;
import com.xoa.model.diary.DiaryModel;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowFormType;
import com.xoa.model.workflow.FlowReport;
import com.xoa.model.workflow.FlowReportPriv;
import com.xoa.model.workflow.TMacroCtrl;
import com.xoa.service.WFE.WFELog.WFELog;

import com.xoa.service.workflow.flowformtype.FlowFormService;
import com.xoa.service.workflow.flowtype.FlowFormTypeService;
import com.xoa.service.workflow.reportSettings.FlowReportService;
import com.xoa.service.workflow.wrapper.FlowFormWrappers;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope(value = "prototype")
@RequestMapping("/reportSettings")
/**
 * 报表设置Controller
 * 王禹萌
 * 2018-07-30
 */
public class FlowReportController {
	@Resource
	private FlowReportService flowReportService;

	/**
	 * 报表设置映射
	 * @param model
	 * @param request
	 * @param formId
	 * @return
	 */
	@RequestMapping("/reportSettingsList")
	public String previews(Model model,HttpServletRequest request,Integer formId) {
		Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
		model.addAttribute("formId",formId);
		return "app/workflow/reportSettings/reportSettingsList";
	}

	@RequestMapping("/index")
	public String index(Model model,HttpServletRequest request,Integer formId) {
		Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
		model.addAttribute("formId",formId);
		return "app/workflow/reportSettings/index";
	}

	@RequestMapping("/qxSetting")
	public String qxSetting(Model model,HttpServletRequest request,Integer formId) {
		Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
		model.addAttribute("formId",formId);
		return "app/workflow/reportSettings/qxSetting";
	}

	@RequestMapping("/newReport")
	public String newReport(Model model,HttpServletRequest request,Integer formId) {
		Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
		model.addAttribute("formId",formId);
		return "app/workflow/reportSettings/newReport";
	}

	//cs流程报表页面
	@RequestMapping("/powerFlowReport/demoRpt")
	public String demoRpt() {
		return "app/workflow/reportSettings/powerFlowReport/demoRpt";
	}

	//cs流程报表页面
	@RequestMapping("/powerFlowReport/personflowview")
	public String personflowview() {
		return "app/workflow/reportSettings/powerFlowReport/personflowview";
	}
	/**
	 * cs-考核报告部门信息报表设置映射
	 * @return
	 */
	@RequestMapping("/powerFlowReport/khbgDeptRptView")
	public String khbgDeptRptViews() {
		return "app/workflow/reportSettings/powerFlowReport/khbgDeptRpt";
	}
	/**
	 * 新建报表设置
	 * 王禹萌
	 * 2018-08-01 14：06
	 * @param tid 未知字段
	 * @param flowId 流程Id
	 * @param rName 报表名称
	 * @param groupType 统计方式
	 * @param groupField 分组字段
	 * @return
	 */
	@RequestMapping("/newReportSettings")
	@ResponseBody
	public ToJson<FlowReport> newReportSettings(HttpServletRequest request,
												@RequestParam(required = false)Integer tid,
												@RequestParam(required = false)String formId,
												@RequestParam(required = false)String flowId,
												@RequestParam(required = false)String rName,
												@RequestParam(value = "listItem[]")String listItem[],
												@RequestParam(value = "queryItem[]")String queryItem[],
												@RequestParam(required = false)String groupType,
												@RequestParam(required = false)String groupField){

		Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
		Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users
				(),redisSessionCookie);
		String createUser = user.getUserId();
		return flowReportService.newReportSettings
				(tid,flowId,rName,listItem,queryItem,createUser,groupType,groupField,formId);

	}

	/**
	 * 报表设置编辑查询
	 * 王禹萌
	 * 2018-08-13 14：14
	 * @param rId
	 * @return
	 */
	@RequestMapping("/queryIdReportSettings")
	@ResponseBody
	public ToJson<FlowReport> queryIdReportSettings(Integer rId){
		return flowReportService.queryIdReportSettings(rId);
	}
	/**
	 * 报表设置查询
	 * 王禹萌
	 * 2018-07-30 11：27
	 * @param pageSize
	 * @param page
	 * @param useFlag
	 * @param flowId 流程ID
	 * @param rName 报表名称
	 * @return
	 */
	@RequestMapping("/queryReportSettings")
	@ResponseBody
	public ToJson<FlowReport> queryReportSettings( @RequestParam(required = false) Integer pageSize,
												   @RequestParam(required = false) Integer page,
												   @RequestParam(required = false) Boolean useFlag,
												   @RequestParam(required = false)String flowId,
												   @RequestParam(required = false)String rName){
		return flowReportService.queryReportSettings(flowId,rName,page,pageSize,useFlag);
	}

	/**
	 * 删除报表
	 * 王禹萌
	 * 2018-07-30 19：27
	 * @param rId
	 * @return
	 */
	@RequestMapping("/deleteReportSettings")
	@ResponseBody
	public ToJson<FlowReport> deleteReportSettings(Integer rId){
		return flowReportService.deleteReportSettings(rId);
	}

	/**
	 * 编辑报表设置
	 * 王禹萌
	 * 2018-07-30
	 * @param request
	 * @param tid
	 * @param formId
	 * @param flowId
	 * @param rName
	 * @param listItem
	 * @param queryItem
	 * @param groupType
	 * @param groupField
	 * @return
	 */
	@RequestMapping("/updateReportSettings")
	@ResponseBody
	public ToJson<FlowReport> updateReportSettings(HttpServletRequest request,
												   @RequestParam(required = false)Integer rId,
												   @RequestParam(required = false)Integer tid,
												   @RequestParam(required = false)String formId,
												   @RequestParam(required = false)String flowId,
												   @RequestParam(required = false)String rName,
												   @RequestParam(value = "listItem[]")String listItem[],
												   @RequestParam(value = "queryItem[]")String queryItem[],
												   @RequestParam(required = false)String groupType,
												   @RequestParam(required = false)String groupField){

		Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
		Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users
				(),redisSessionCookie);
		String createUser =user.getUserId();
		return flowReportService.updateReportSettings(rId,tid,flowId,rName,listItem,queryItem,createUser,groupType,groupField,formId);

	}

	/**
	 *根据流程Id查出分组字段
	 * 王禹萌
	 * 2018-07-30 21：07
	 * @param flowId
	 * @return
	 */
	@RequestMapping("/querybyFlowIdField")
	@ResponseBody
	public ToJson<FlowFormType> querybyFlowIdField(String flowId){
		return flowReportService.querybyFlowIdField(flowId);
	}


	/**
	 * 新建报表设置权限
	 * 	王禹萌
	 * 	2018-07-31 10：51
	 * @param rId 报表Id
	 * @param userStr 用户权限字符串
	 * @param deptStr 部门权限字符串
	 * @return
	 */
	@RequestMapping("/newReportPermission")
	@ResponseBody
	public ToJson<FlowReportPriv> newReportPermission(String rId, String userStr, String deptStr){
		return flowReportService.newReportPermission(rId,userStr,deptStr); 
	}

	/**
	 * 根据报表Id查询所有权限 
	 * 王禹萌
	 * 2018-07-31 17：15
	 * @param rId
	 * @return
	 */
	@RequestMapping("/queryReportPermission")
	@ResponseBody
	public ToJson<FlowReportPriv> queryReportPermission(String rId){
		return flowReportService.queryReportPermission(rId);
	}

	/**
	 * 修改报表设置权限
	 * 王禹萌
	 * 2018-07-31
	 * @param rId 报表Id
	 * @return
	 */
	@RequestMapping("/updateReportPermission")
	@ResponseBody
	public ToJson<FlowReportPriv> updateReportPermission(String rId,String pId, String userStr, String deptStr){
		return flowReportService.updateReportPermission(rId,pId,userStr,deptStr);
	}

	/**
	 * 删除报表设置权限
	 * @param rId
	 * @return
	 */
	@RequestMapping("/deleteReportPermission")
	@ResponseBody
	public ToJson<FlowReportPriv> deleteReportPermission(String rId,String id){
		return flowReportService.deleteReportPermission(id,rId);
	}


	/**
	 * 编辑按钮后回显查询
	 * 王禹萌
	 * 2018-08-01 10：01
	 * @param rId
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectReportPermission")
	@ResponseBody
	public ToJson<FlowReportPriv> selectReportPermission(String rId,String id){
		return flowReportService.selectReportPermission(rId,id);
	}



}
